package com.demo.miya.controller;

import com.alibaba.fastjson.JSON;
import com.demo.miya.controller.client.MiyaClient;
import com.demo.miya.controller.client.MiyaHelloClient;
import com.demo.miya.dto.GeneralResult;
import com.demo.miya.model.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * @author K
 */
@Slf4j
@RestController
public class ServiceMiyaController implements MiyaClient, MiyaHelloClient {

	@Override
	public String hi() {
		log.info("service-miya服务hi接口被调用");
		return "hi i'm miya!";
	}

	@Override
	public String hello() {
		log.info("service-miya服务hello接口被调用");
		return "hello i'm miya!";
	}

	@PostMapping("/respBody")
	public String respBody(@RequestBody User user) throws InterruptedException {
		log.info("请求参数：{}", JSON.toJSONString(user));
		Thread.sleep(1000L);
		user.setId(1L);
		user.setUserId(2L);
		user.setUserName("userName");
		return JSON.toJSONString(user);
	}

	/**
	 * luna服务使用retrofit调用
	 * @param user 请求参数
	 * @return 修改过的User对象
	 */
	@PostMapping("/modifyUser")
	public User modifyUser(@RequestBody User user) {
		user.setUserId(11L);
		user.setUserName("11");
		return user;
	}

	@PostMapping("/initUser")
	public GeneralResult<User> initUser(@RequestBody User user) {
		user.setId(22L);
		user.setUserName("22");
		return GeneralResult.success(user);
	}

	@PostMapping("/initPerson")
	public GeneralResult<Map<String, Object>> initPerson() {
		Map<String, Object> map = new HashMap<>(8);
		map.put("Name", "*name*");
		map.put("Age", 10);
		map.put("Sex", "男");
		return GeneralResult.success(map);
	}

	@PostMapping("/respBodyException")
	public String respException(@RequestBody User user) throws Exception {
		log.info("请求参数：{}", JSON.toJSONString(user));
		Thread.sleep(1000L);
		throw new Exception("异常");
	}
}
